Skip to content

Conversation

ohad-nir-starkware
Copy link
Collaborator

@ohad-nir-starkware ohad-nir-starkware commented Jan 29, 2025

Blake2sLastBlock opcode runner

Description

Adding the opcode Blake2sLastBlock to the VM.
Expects op0 to be a pointer to a sequence of 9 felts and and op1 to be a pointer to a sequence of 16 felts.
Said felts should represent u32 integers, i.e. have value of at most 2**32-1.
The first 8 felts of op0 represent a state, the 9th represents the counter and the 10th represents counter+n_bytes i.e. the total length of the entire message.
The 16 felts of op1 represent a message.
The "output" consists of 8 felts representing u32 numbers of the Blake2s compression of the last block.
dst should be a pointer, it points to a sequence of 8 cells which each should either be uninitialised or already contain a value matching that of the output at the same index.
The opcode inserts the aforementioned output into the 8 cells [dst], [dst+1], ... [dst+7] (and yields an error if one of said cells already contains a value differing from the output).

Currently Blake2sLastBlock has opcode_num 16, meaning encoded_instr is expanded to 128 bits with the 63 most significant bits expected to be 0 (otherwise yield an error).

The motivation is that it has been decided at Starkware that Blake2sLastBlock is to be implemented at an opcode, thus it needs to be supported by the runner.

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.
    • CHANGELOG has been updated.

This change is Reviewable

@ohad-nir-starkware ohad-nir-starkware self-assigned this Jan 29, 2025
@ohad-nir-starkware ohad-nir-starkware changed the base branch from main to ohadn/blake2s_opcode_runner January 29, 2025 14:48
Copy link

github-actions bot commented Jan 29, 2025

**Hyper Thereading Benchmark results**




hyperfine -r 2 -n "hyper_threading_main threads: 1" 'RAYON_NUM_THREADS=1 ./hyper_threading_main' -n "hyper_threading_pr threads: 1" 'RAYON_NUM_THREADS=1 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 1
  Time (mean ± σ):     27.999 s ±  0.010 s    [User: 27.246 s, System: 0.750 s]
  Range (min … max):   27.992 s … 28.005 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     27.341 s ±  0.024 s    [User: 26.576 s, System: 0.762 s]
  Range (min … max):   27.324 s … 27.358 s    2 runs
 
Summary
  hyper_threading_pr threads: 1 ran
    1.02 ± 0.00 times faster than hyper_threading_main threads: 1




hyperfine -r 2 -n "hyper_threading_main threads: 2" 'RAYON_NUM_THREADS=2 ./hyper_threading_main' -n "hyper_threading_pr threads: 2" 'RAYON_NUM_THREADS=2 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 2
  Time (mean ± σ):     15.544 s ±  0.020 s    [User: 27.414 s, System: 0.782 s]
  Range (min … max):   15.530 s … 15.558 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     15.273 s ±  0.085 s    [User: 26.765 s, System: 0.775 s]
  Range (min … max):   15.213 s … 15.334 s    2 runs
 
Summary
  hyper_threading_pr threads: 2 ran
    1.02 ± 0.01 times faster than hyper_threading_main threads: 2




hyperfine -r 2 -n "hyper_threading_main threads: 4" 'RAYON_NUM_THREADS=4 ./hyper_threading_main' -n "hyper_threading_pr threads: 4" 'RAYON_NUM_THREADS=4 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 4
  Time (mean ± σ):     11.239 s ±  0.017 s    [User: 41.180 s, System: 0.917 s]
  Range (min … max):   11.227 s … 11.251 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     10.772 s ±  0.303 s    [User: 40.158 s, System: 0.925 s]
  Range (min … max):   10.558 s … 10.987 s    2 runs
 
Summary
  hyper_threading_pr threads: 4 ran
    1.04 ± 0.03 times faster than hyper_threading_main threads: 4




hyperfine -r 2 -n "hyper_threading_main threads: 6" 'RAYON_NUM_THREADS=6 ./hyper_threading_main' -n "hyper_threading_pr threads: 6" 'RAYON_NUM_THREADS=6 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 6
  Time (mean ± σ):     11.234 s ±  0.055 s    [User: 40.729 s, System: 0.927 s]
  Range (min … max):   11.195 s … 11.272 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     10.852 s ±  0.114 s    [User: 39.486 s, System: 0.939 s]
  Range (min … max):   10.771 s … 10.932 s    2 runs
 
Summary
  hyper_threading_pr threads: 6 ran
    1.04 ± 0.01 times faster than hyper_threading_main threads: 6




hyperfine -r 2 -n "hyper_threading_main threads: 8" 'RAYON_NUM_THREADS=8 ./hyper_threading_main' -n "hyper_threading_pr threads: 8" 'RAYON_NUM_THREADS=8 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 8
  Time (mean ± σ):     11.120 s ±  0.210 s    [User: 41.344 s, System: 0.941 s]
  Range (min … max):   10.971 s … 11.269 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     10.755 s ±  0.082 s    [User: 39.941 s, System: 0.969 s]
  Range (min … max):   10.697 s … 10.812 s    2 runs
 
Summary
  hyper_threading_pr threads: 8 ran
    1.03 ± 0.02 times faster than hyper_threading_main threads: 8




hyperfine -r 2 -n "hyper_threading_main threads: 16" 'RAYON_NUM_THREADS=16 ./hyper_threading_main' -n "hyper_threading_pr threads: 16" 'RAYON_NUM_THREADS=16 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 16
  Time (mean ± σ):     11.248 s ±  0.115 s    [User: 41.511 s, System: 1.007 s]
  Range (min … max):   11.167 s … 11.329 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     10.896 s ±  0.212 s    [User: 40.424 s, System: 1.037 s]
  Range (min … max):   10.746 s … 11.046 s    2 runs
 
Summary
  hyper_threading_pr threads: 16 ran
    1.03 ± 0.02 times faster than hyper_threading_main threads: 16


Copy link

github-actions bot commented Jan 29, 2025

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.187 ± 0.017 2.173 2.229 1.00
head big_factorial 2.213 ± 0.033 2.183 2.276 1.01 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.150 ± 0.023 2.127 2.197 1.00
head big_fibonacci 2.156 ± 0.019 2.136 2.198 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 7.963 ± 0.028 7.925 7.998 1.00
head blake2s_integration_benchmark 8.254 ± 0.079 8.069 8.342 1.04 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.260 ± 0.016 2.239 2.292 1.00
head compare_arrays_200000 2.292 ± 0.037 2.260 2.391 1.01 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.494 ± 0.012 1.485 1.527 1.00 ± 0.01
head dict_integration_benchmark 1.487 ± 0.009 1.478 1.507 1.00
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.271 ± 0.010 1.258 1.295 1.00
head field_arithmetic_get_square_benchmark 1.295 ± 0.018 1.286 1.345 1.02 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 8.035 ± 0.061 7.895 8.090 1.00
head integration_builtins 8.343 ± 0.119 8.129 8.590 1.04 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 8.288 ± 0.075 8.213 8.423 1.00
head keccak_integration_benchmark 8.507 ± 0.121 8.361 8.686 1.03 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.232 ± 0.013 2.216 2.252 1.00
head linear_search 2.260 ± 0.014 2.242 2.293 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.568 ± 0.007 1.559 1.583 1.01 ± 0.01
head math_cmp_and_pow_integration_benchmark 1.556 ± 0.009 1.549 1.580 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.538 ± 0.013 1.527 1.570 1.02 ± 0.01
head math_integration_benchmark 1.507 ± 0.007 1.498 1.523 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.251 ± 0.004 1.245 1.258 1.00
head memory_integration_benchmark 1.254 ± 0.005 1.251 1.266 1.00 ± 0.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.648 ± 0.014 1.629 1.670 1.01 ± 0.01
head operations_with_data_structures_benchmarks 1.629 ± 0.007 1.617 1.641 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 544.8 ± 3.4 541.8 553.9 1.00 ± 0.01
head pedersen 544.8 ± 1.6 542.0 546.6 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 637.0 ± 9.0 630.4 655.1 1.01 ± 0.01
head poseidon_integration_benchmark 631.7 ± 2.5 628.4 635.6 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 1.902 ± 0.010 1.889 1.926 1.00 ± 0.01
head secp_integration_benchmark 1.895 ± 0.013 1.879 1.922 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 639.1 ± 1.6 637.1 641.9 1.01 ± 0.00
head set_integration_benchmark 635.7 ± 1.1 633.5 637.2 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.432 ± 0.046 4.373 4.512 1.00
head uint256_integration_benchmark 4.488 ± 0.041 4.426 4.532 1.01 ± 0.01

Copy link

codecov bot commented Jan 29, 2025

Codecov Report

Attention: Patch coverage is 97.95918% with 2 lines in your changes missing coverage. Please review.

Project coverage is 96.39%. Comparing base (153d3a8) to head (cee3133).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
vm/src/vm/vm_core.rs 97.67% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1932   +/-   ##
=======================================
  Coverage   96.39%   96.39%           
=======================================
  Files         102      102           
  Lines       41483    41566   +83     
=======================================
+ Hits        39987    40068   +81     
- Misses       1496     1498    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch from bcf7dc5 to 4919048 Compare January 29, 2025 16:43
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_opcode_runner branch 2 times, most recently from 2ce852d to d25cef1 Compare January 31, 2025 13:01
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch from 4919048 to 0260570 Compare January 31, 2025 13:05
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_opcode_runner branch 2 times, most recently from 1c031e4 to de91ec1 Compare February 3, 2025 19:44
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_opcode_runner branch 9 times, most recently from b26563b to 246904f Compare February 5, 2025 08:03
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch from 0260570 to 7b7d956 Compare February 5, 2025 11:56
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_opcode_runner branch from 246904f to 4490aab Compare February 5, 2025 12:49
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_opcode_runner branch from 4a7be69 to 62c28ae Compare February 11, 2025 19:25
Copy link

@DavidLevitGurevich DavidLevitGurevich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 9 files at r1, 4 of 4 files at r6, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @Oppen, @pefontana, and @YairVaknin-starkware)


cairo_programs/stwo_exclusive_programs/blake2s_opcode_test.cairo line 138 at r6 (raw file):

    let offset1 = (2**15)-4;
    let offset2 = (2**15)-3;
    static_assert dst == [fp -5];

space after the -


cairo_programs/stwo_exclusive_programs/blake2s_opcode_test.cairo line 161 at r6 (raw file):

    let flag_num = flag_dst_base_fp+flag_op0_base_fp*(2**1)+flag_op1_imm*(2**2)+flag_op1_base_fp*(2**3);
    let blake2s_opcode_extension_num = 1;
    let blake2s_last_block_opcode_extension_num =2;

missing space


cairo_programs/stwo_exclusive_programs/blake2s_opcode_test.cairo line 167 at r6 (raw file):

    static_assert blake2s_last_block_instruction_num==18449981025204076539;

    // Write the instruction to [pc] and point [ap+1] to the designated output.

how did the +1 pop up? if the reason is hidden in the if statement I would replace it with the plane jmp syntax

@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_opcode_runner branch from 62c28ae to 4f2ec51 Compare February 12, 2025 07:58
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch from 0e3fb2c to 330f44d Compare February 12, 2025 08:03
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_opcode_runner branch from 4f2ec51 to 261b2f7 Compare February 12, 2025 10:11
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch 2 times, most recently from d707312 to 2190f0d Compare February 12, 2025 11:56
Copy link
Collaborator Author

@ohad-nir-starkware ohad-nir-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 2 of 11 files reviewed, 4 unresolved discussions (waiting on @DavidLevitGurevich, @fmoletta, @gabrielbosio, @igaray, @juanbono, @Oppen, @pefontana, and @YairVaknin-starkware)


cairo_programs/stwo_exclusive_programs/blake2s_opcode_test.cairo line 138 at r6 (raw file):

Previously, DavidLevitGurevich wrote…

space after the -

Done.


cairo_programs/stwo_exclusive_programs/blake2s_opcode_test.cairo line 161 at r6 (raw file):

Previously, DavidLevitGurevich wrote…

missing space

Done.


cairo_programs/stwo_exclusive_programs/blake2s_opcode_test.cairo line 167 at r6 (raw file):

Previously, DavidLevitGurevich wrote…

how did the +1 pop up? if the reason is hidden in the if statement I would replace it with the plane jmp syntax

Done.

Copy link

@DavidLevitGurevich DavidLevitGurevich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 9 files at r7.
Reviewable status: 5 of 11 files reviewed, 1 unresolved discussion (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @Oppen, @pefontana, and @YairVaknin-starkware)

Copy link

@DavidLevitGurevich DavidLevitGurevich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: 5 of 11 files reviewed, 1 unresolved discussion (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @Oppen, @pefontana, and @YairVaknin-starkware)

@ohad-nir-starkware ohad-nir-starkware changed the base branch from ohadn/blake2s_opcode_runner to main February 12, 2025 18:06
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch from 2190f0d to 2a632d3 Compare February 12, 2025 20:07
Copy link

@DavidLevitGurevich DavidLevitGurevich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 3 files at r8, all commit messages.
Reviewable status: 7 of 11 files reviewed, 1 unresolved discussion (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @Oppen, @pefontana, and @YairVaknin-starkware)

@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch 2 times, most recently from d0711e3 to c52927b Compare February 15, 2025 08:24
@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch 3 times, most recently from 63e63fa to b330037 Compare February 15, 2025 12:55
&message,
counter,
0,
if !is_last_block { 0 } else { 0xffffffff },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract this logic into a variable next to the definitions of state and message (lines 481-497)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 114 to 123
if (opcode_extension == OpcodeExtension::Blake
|| opcode_extension == OpcodeExtension::BlakeFinalize)
&& (opcode != Opcode::NOp
|| (op1_addr != Op1Addr::FP && op1_addr != Op1Addr::AP)
|| res != Res::Op1
|| pc_update != PcUpdate::Regular
|| (ap_update_num != 0 && ap_update_num != 2))
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract these conditionals into a variable to separate the opcode_extension checks of the flags check. Something like:

Suggested change
if (opcode_extension == OpcodeExtension::Blake
|| opcode_extension == OpcodeExtension::BlakeFinalize)
&& (opcode != Opcode::NOp
|| (op1_addr != Op1Addr::FP && op1_addr != Op1Addr::AP)
|| res != Res::Op1
|| pc_update != PcUpdate::Regular
|| (ap_update_num != 0 && ap_update_num != 2))
{
let are_blake_flags_invalid = opcode != Opcode::NOp
|| (op1_addr != Op1Addr::FP && op1_addr != Op1Addr::AP)
|| res != Res::Op1
|| pc_update != PcUpdate::Regular
|| (ap_update_num != 0 && ap_update_num != 2;
if (opcode_extension == OpcodeExtension::Blake
|| opcode_extension == OpcodeExtension::BlakeFinalize)
&& are_opcodes_flags_invalid)
{

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@ohad-nir-starkware ohad-nir-starkware force-pushed the ohadn/blake2s_last_block_opcode_VM branch from b330037 to cee3133 Compare February 17, 2025 15:23
Copy link
Collaborator Author

@ohad-nir-starkware ohad-nir-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 5 of 11 files reviewed, 3 unresolved discussions (waiting on @DavidLevitGurevich, @fmoletta, @gabrielbosio, @igaray, @juanbono, @Oppen, @pefontana, @YairVaknin-starkware, and @yuvalsw)

&message,
counter,
0,
if !is_last_block { 0 } else { 0xffffffff },
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 114 to 123
if (opcode_extension == OpcodeExtension::Blake
|| opcode_extension == OpcodeExtension::BlakeFinalize)
&& (opcode != Opcode::NOp
|| (op1_addr != Op1Addr::FP && op1_addr != Op1Addr::AP)
|| res != Res::Op1
|| pc_update != PcUpdate::Regular
|| (ap_update_num != 0 && ap_update_num != 2))
{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@gabrielbosio gabrielbosio added this pull request to the merge queue Feb 17, 2025
Copy link

@DavidLevitGurevich DavidLevitGurevich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 9 files at r7, 2 of 3 files at r9, 2 of 2 files at r10, all commit messages.
Reviewable status: 10 of 11 files reviewed, 3 unresolved discussions (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @Oppen, @pefontana, @YairVaknin-starkware, and @yuvalsw)

Merged via the queue into main with commit abe5f44 Feb 17, 2025
93 checks passed
@gabrielbosio gabrielbosio deleted the ohadn/blake2s_last_block_opcode_VM branch February 17, 2025 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants